﻿import flash.external.ExternalInterface;

if (!this.objectName) {
	this.objectName = "test";
}
if (!this.serverUrl) {
	this.serverUrl = "rtmp://pro-web.at/SOSample";
}
persistent = this.persisent ? true: false;

this.txtServerUrl = this.serverUrl;
this.txtObjectName = this.objectName;

// create basic netConnection object
var nc:NetConnection = new NetConnection();

// connect to the local Red5 server
nc.connect(this.serverUrl);

// create StoredObject - pass the name of the shared object, the netConnection URI ("rtmp://localhost/oflaDemo"), and whether or not it's persistant
var so:SharedObject = SharedObject.getRemote(this.objectName, nc.uri, persistent);

// this method is called when the data on the Shared Object is updated
so.onSync = function(evtObj) {
	// merge all data entries in seperate object for client
	var data = {};
	for (key in so.data) {
		data[key] = so.data[key];
	}

	// merge all updates in a nicer way for client
	var updates = {};
	for (key in evtObj) {
		if (evtObj[key].name) {
			updates[evtObj[key].name] = evtObj[key].code;
		}
	}

	txtLog = "sync";
	ExternalInterface.call("jsSO.sync", updates, data);
};

// connect to the SO using the netConnection reference
so.connect(nc);


function get(item)
{
	return so.data[item];
}
function set(item, data):Void
{
	so.data[item] = data;
}
ExternalInterface.addCallback("set", null, set);
ExternalInterface.addCallback("get", null, get);
ExternalInterface.call("jsSO.flashInit");
